package com.hong.cloud.mall.practice.cartorder.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 描述：     生成订单No工具类
 *
 */
public class OrderCodeFactory {
    private static ThreadLocal<SimpleDateFormat> dateFormatThreadLocal = new ThreadLocal(){
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyyMMddHHmmss");
        }
    };
    private static String getDateTime() {
        DateFormat sdf = dateFormatThreadLocal.get();
        return sdf.format(new Date());
    }
    private static int getRandom(Long n) {
        Random random = new Random();
        // 获取5位随机数
        return (int) (random.nextDouble() * (90000)) + 10000;
    }
    public static String getOrderCode(Long userId) {
        return getDateTime() + getRandom(userId);
    }
}
